package com.study.edoc.controller;

import com.study.edoc.exception.R;
import com.study.edoc.pojo.UserInfo;
import com.study.edoc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/v1/user")
@Api(tags = "用户管理接口",description ="用户管理模块相关接口" )
public class UserController {

    @Resource
    private UserService userService;


    @PostMapping("/register")
    /*
    * @ApiOperation：用在请求的方法上，说明方法的作用
    value="说明方法的作用"
    notes="方法的备注说明"
    * */
    @ApiOperation(value = "用户注册",notes = "用户注册接口")
    /*
    *  @ApiImplicitParam：用在@ApiImplicitParams注解中，指定一个请求参数的各个方面
        name：参数名
        value：参数的中文说明
        required：参数是否必须传
        paramType：参数放在哪个地方
            · header --> 请求参数的获取：@RequestHeader
            · query --> 请求参数的获取：@RequestParam
            · path（用于restful接口）--> 请求参数的获取：@PathVariable
            · body（不常用）
            · form（不常用）
        dataType：参数类型，默认String，其它值dataType="Integer"
        defaultValue：参数的默认值
    * */
    @ApiImplicitParam(name = "userInfo",value = "用户实体",required=true,paramType = "body",dataType = "UserInfo")
    public R register(@RequestBody UserInfo userInfo){
        boolean retVal = userService.register(userInfo);
        if(retVal){
            return R.success(0,"用户注册成功。");
        }
        return R.fail("用户注册失败。");

    }



    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value = "用户登录", notes = "用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "string", paramType = "query", example = "13802780104", required = true),
            @ApiImplicitParam(name = "password", value = "登录密码", dataType = "string", paramType = "query", example = "123456", required = true)
    })
    public R login(@RequestParam(value = "phone") String phone,
                   @RequestParam(value = "password") String password){
        UserInfo user = userService.login(phone, password);
        if(user == null){
            return R.error(0,"登录失败，账号或密码错误,请重新输入。");
        }
        return R.success(user);
    }

}
